/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import local.SystemUserFacadeLocal;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import model.Role;
import model.SystemUser;

/**
 *
 * @author karennyq
 */
@Stateless
public class SystemUserFacade implements SystemUserFacadeLocal {

    @PersistenceContext(unitName = "IEMPPU")
    private EntityManager em;

    @Override
    public Collection findAllSysUser(Integer page, Integer rows, String sort, String order) {
        Query query = em.createQuery("SELECT su FROM SystemUser su ORDER BY su." + sort + " " + order);
        return (Collection) query.getResultList();
    }

    @Override
    public int countAllSysUser(Integer page, Integer rows, String sort, String order) {
        Query query = em.createQuery("SELECT su FROM SystemUser su ORDER BY su." + sort + " " + order);
        return query.getResultList().size();
    }

    @Override
    public boolean createSysUser(SystemUser su) {
        try {
            em.persist(su);
            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    @Override
    public boolean checkForExistence(String email) {
        Query query = em.createQuery("SELECT su FROM SystemUser su WHERE su.email=?1");
        query.setParameter(1, email);

        ArrayList<SystemUser> su = new ArrayList(query.getResultList());
        if (su.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean checkForExistence(Long sys_user_id) {
        SystemUser su = em.find(SystemUser.class, sys_user_id);

        if (su == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public SystemUser getSysUser(Long sys_user_id) {
        SystemUser su = em.find(SystemUser.class, sys_user_id);
        return su;
    }

    @Override
    public void deleteSysUser(Long sys_user_id) {
        SystemUser su = em.find(SystemUser.class, sys_user_id);
        em.remove(su);
    }

    @Override
    public SystemUser resetPwd(Long sys_user_id, String new_hashed_pwd) {
        SystemUser su = em.find(SystemUser.class, sys_user_id);
        su.setPassword(new_hashed_pwd);
        em.merge(su);
        return su;
    }

    @Override
    public SystemUser updateSysUser(Long sys_user_id, String email, Role role, String name) {
        SystemUser su = em.find(SystemUser.class, sys_user_id);
        su.setEmail(email);
        su.setRole(role);
        su.setName(name);
        em.merge(su);
        return su;
    }

    @Override
    public SystemUser updateSysUser(SystemUser suInfo) {
        SystemUser su = em.find(SystemUser.class, suInfo.getSys_user_id());
        su.setPassword(suInfo.getPassword());
        em.merge(su);
        return su;
    }

    @Override
    public SystemUser getCredentials(String username, String password) throws NoSuchAlgorithmException {
        Query query = em.createQuery("SELECT su FROM SystemUser su WHERE su.email=?1");
        query.setParameter(1, username);

        if (query.getResultList().isEmpty()) {
            return null;
        } else {
            SystemUser su = (SystemUser) query.getSingleResult();

            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(password.getBytes());
            byte byteData[] = md.digest();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < byteData.length; i++) {
                sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }

            if (su.getPassword().equals(sb.toString())) {
                return su;
            } else {
                return null;
            }
        }
    }
}
